package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.studentService;

import java.sql.SQLOutput;
import java.util.Scanner;

public class studentController {
   private Scanner sc=new Scanner(System.in);
   private studentService StudentService=new studentService();



    //同来开启学生管理系统，展示菜单
    public void start() {

       lo: while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");

            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                   // System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updeteStudent();
                    break;
                case "4":
                   // System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统,再见");
                    break lo;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
}

    public void updeteStudent() {
        Student[] stus =StudentService.findAllStudent();

        //判断数组信息是否为NUll
        if (stus==null){
            System.out.println("查无信息");
            return;}
        String updeteid=inputStudentId();


        /*System.out.println("请输入学生姓名：");
        String name=sc.next();
        System.out.println("请输入学生年龄：");
        String age=sc.next();
        System.out.println("请输入学生生日：");
        String birthday=sc.next();

        Student newstu=new Student();
        newstu.setId(updeteid);
        newstu.setName(name);
        newstu.setAge(age);
        newstu.setBirthday(birthday);*/
        Student newstu=inputStudentInfo(updeteid);
StudentService.updateStudent(updeteid,newstu);
        System.out.println("修改成功");
    }

    public void deleteStudent() {
        Student[] stus =StudentService.findAllStudent();

        //判断数组信息是否为NUll
        if (stus==null){
            System.out.println("查无信息");
            return;
        }
        String deleteid=inputStudentId();




        //调用业务员delete方法
        StudentService.deletestudent(deleteid);
        System.out.println("删除成功");
    }

    public void findAllStudent() {
        //调用业务员中的获取方法 得到学生数组
        Student[] stus =StudentService.findAllStudent();

        //判断数组信息是否为NUll
        if (stus==null){
            System.out.println("查无信息");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        //便利数组
        for (int i = 0; i < stus.length; i++) {
            Student stu=stus[i];
            if (stu!=null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t"+stu.getBirthday());
            }
            
        }
        

    }

    public void addStudent() {
        //键盘接收学生信息 再把信息封装为学生对象
        //把学生对象传递给studentService（业务员）中的addstudent方法
        //根据返回的BOOLEAN类型结果，在控制台打印成功/失败

       // studentService StudentService=new studentService();
        String id;
        while (true) {
            System.out.println("请输入学生ID：");
             id = sc.next();
           boolean flag= StudentService.isExists(id);
           if (flag){
               System.out.println("Id已被占用");
           }else{
               break;
           }
        }
        /*System.out.println("请输入学生姓名：");
        String name =sc.next();
        System.out.println("请输入学生年龄：");
        String age =sc.next();
        System.out.println("请输入学生生日：");
        String birthday =sc.next();
        Student stu =new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);*/
        Student stu=inputStudentInfo(id);

        boolean result = StudentService.addStudent(stu);
        if (result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }



    }


    //键盘录入学生Id
    public String inputStudentId(){
        String id;
        while (true) {
            System.out.println("请输入学生ID");
            //键盘录入要删除的学生
          id = sc.next();

            //判断id是否存在}\
            boolean exists= StudentService.isExists(id);
            if (!exists){
                System.out.println("您输入的ID不存在 请重新输入");
            }else{
                break;
            }


    }
        return id;
}
public Student inputStudentInfo(String id){
    System.out.println("请输入学生姓名：");
    String name=sc.next();
    System.out.println("请输入学生年龄：");
    String age=sc.next();
    System.out.println("请输入学生生日：");
    String birthday=sc.next();

    Student stu=new Student();
    stu.setId(id);
    stu.setName(name);
    stu.setAge(age);
    stu.setBirthday(birthday);
    return stu;



}



}